<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<fsButton ref="button" plain size="default" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
			</div>
			<div class="right-panel">
				<ex-search ref="search" :searches="searches" @searchOk="searchHandler" default-code="searchKeyword"
						   placeholder="请输入搜索关键字" :fixed-param="{excludeStatus:['delete']}"></ex-search>
			</div>
		</el-header>
		<el-main class="nopadding">
			<el-container>
				<!--		  <el-aside class="nopadding"  style="min-width:300px;width: 18%;">-->
				<!--			<ex-search-tree ref="searchTree" :treeApi="this.$API.asset.column.treeData" @changeHandler="searchTreeHandler" :param="{isGroup:'1'}">-->
				<!--			</ex-search-tree>-->
				<!--		  </el-aside>-->
				<el-main class="nopadding">
					<ex-table id="assetTable" ref="table" :columns="columns" :url="this.$API.asset.asset.list.url"
							  :refs="this.$refs" search="search" :params="{deptIds:this.$TOOL.current.readDeptIds(),assetCategoryIds:this.$TOOL.current.readAssetCategoryIds()}" @columnClick="columnClickHandler"></ex-table>
				</el-main>
			</el-container>
		</el-main>
	</el-container>
	<data-card ref="dataCard"></data-card>
	<print-review ref="printReview"></print-review>
	<import-tags ref="importTags"></import-tags>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExTreeNavigation from "@/components/exSearchTree/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import DataCard from "@/views/asset/card/card.vue";
import PrintReview from "@/views/asset/labelPrint/printReview.vue";
import ImportTags from "@/views/asset/labelPrint/importTags.vue";
import globalparam from "@/constant/model/globalparam";
import BaseTemplate from "@/utils/baseTemplate";
//import dict from "@/utils/dict";

export default {
	name: '标签打印',
	components: {
		ImportTags,
		PrintReview,
		DataCard,
		ExSearchTree,
		ExTreeNavigation,
		ExSearch,
		ExTable,
	},
	data() {
		return {
			dialog: {
				save: false,
				permission: false
			},
			apiObj: [],
			buttons: [
				{name: this.$t('button.export'), icon: "el-icon-download", click: "export",type: "info", permission: "export"},
				{name: this.$t('button.import'), icon: "el-icon-upload", click: "import",type: "info", permission: "import"},
				{name: this.$t('button.label_print'), icon: "el-icon-printer",type: "info", click: "labelPrint", permission: "labelPrint"},
			],
			searches: [
				{type: "input", name: "资产名称", code: "name", placeholder: "请输入资产名称",},
				{type: "input", name: "资产编号", code: "code", placeholder: "请输入资产编号",maxlength: 30, showLimit: true},
				{type: "input", name: "财务编码", code: "financialCode", placeholder: "请输入财务编码"},
				{type: "input", name: "财务条形码", code: "other.financialBarCode", placeholder: "请输入财务编码"},
				{type: "select", name: "账务类型", code: "other.financialType", placeholder: "请选择账务类型", dictCode:'FINANCIAL_TYPE'},
				{type: "select", name: "借用类型", code: "borrowType", placeholder: "请选择借用类型", dictCode:'ASSET_BORROW_TYPE'},
				{type: "input", name: "入库单号", code: "other.inDocumentCode", placeholder: "请输入入库单号"},
				{type: "input", name: "申购单号", code: "other.purchaseOrderCode", placeholder: "请输入申购单号"},
				{type: "tree-select", name: "资产分类", code: "assetCategoryId",dictCode:'FamsAssetCategory',priKey:true},
				{type: "input", name: "规格型号", code: "model", placeholder: "请输入规格型号"},
				{type: "input", name: "品牌", code: "brand", placeholder: "请输入品牌"},
				{type: "tree-select", name: "管理部门", code: "dutyDepartmentId",dictCode:'SysOrganization',priKey:true},
				{type: "selector", name: "管理人", code: "dutyMenId",dictCode:'SysUser'},
				{type: "tree-select", name: "使用部门", code: "usingDepartmentId",dictCode:'SysOrganization',priKey:true},
				{type: "selector", name: "使用人", code: "usingMenId",dictCode:'SysUser'},
				{type: "tree-select", name: "借用部门", code: "other.borrowingDepartmentId",dictCode:'SysOrganization',priKey:true},
				{type: "selector", name: "借用人", code: "other.borrowingMenId",dictCode:'SysUser'},
				{type: "select", name: "资产状态", code: "assetStatusId", placeholder: "请选择资产状态", dictCode:'ASSET_STATUS'},
				{type: "tree-select", name: "位置", code: "storePlaceId",dictCode:'FamsPosition'},
				{type: "daterange", name: "入库日期", code: "zcrkDateStart,zcrkDateEnd",valueFormat:"YYYYMMDD",format:"YYYY-MM-DD",range:30},
				{type: "daterange", name: "购置日期", code: "boughtDateStart,boughtDateEnd",valueFormat:"YYYYMMDD",format:"YYYY-MM-DD",range:30},
				{type: "daterange", name: "闲置日期", code: "zcxzDateStart,zcxzDateEnd",valueFormat:"YYYYMMDD",format:"YYYY-MM-DD",range:30},
				{type: "daterange", name: "借用日期", code: "zcjyDateStart,zcjyDateEnd",valueFormat:"YYYYMMDD",format:"YYYY-MM-DD",range:30},
				{type: "daterange", name: "报废日期", code: "scrapDateStart,scrapDateEnd",valueFormat:"YYYYMMDD",format:"YYYY-MM-DD",range:30},
			],
			columns: [
				{code: "assetStatusId", name: "资产状态", width: "65", formatter: "DICT", key: "ASSET_STATUS"},
				{code: "code", name: "资产编码", width: "135", click: "show1"},
				{code: "financialCode", name: "财务编码", width: "170"},
				{code: "name", name: "资产名称", width: "200"},
				{code: "storePlaceId", name: "资产位置", width: "120", formatter: "DICT", key: "FamsPosition"},
				{code: "mainCategoryCode", name: "资产大类",width:"130", formatter:"DICT",key: "FamsAssetCategory"},
				{code: "assetCategoryId", name: "资产分类",width:"150", formatter:"DICT",key: "FamsAssetCategory"},
				{code: "other.financialType", name: "账务类型",width: "75", formatter:"DICT",key: "FINANCIAL_TYPE"},
				{code: "other.financialBarCode", name: "财务条形码", width: "120"},
				{code: "other.purchaseOrderCode", name: "申购单号", width: "150"},
				{code: "brand", name: "品牌", width: ""},
				{code: "model", name: "规格型号", width: ""},
				{code: "serialCode", name: "资产序列号", width: "150"},
				{code: "originalValue", name: "原值", width: "100", summary: true},
				{code: "netValue", name: "净值", width: "100", summary: true},
				{code: "usingMenCode", name: "使用人工号", width: "100"},
				{code: "usingMenId", name: "使用人", width: "80", formatter: "DICT", key: "SysUser"},
				{code: "usingDepartmentId", name: "使用部门", width: "150", formatter: "DICT", key: "SysOrganization"},
			],
			maxSelectdNumber: 0,
			parentLevelCode: "",
			assetIds: [],
		}
	},
	created() {
	},
	mounted() {
		this.$cache.data.loadGlobalParam(globalparam.defaultSelectNumber.paramCode).then((res) => {
			this.maxSelectdNumber = res;
		})
	},
	methods: {
		buttonHandler(index, name) {
			this.$options.methods[name](this, index);
		},
		async searchTreeHandler(data) {
			let queryParams = {pathCode: data.levelCode}
			this.parentLevelCode = data.levelCode;
			await this.$refs.table.refresh(queryParams);
		},

		async searchHandler() {
			let queryParams = {pathCode: this.parentLevelCode}
			await this.$refs.table.refresh(queryParams);
		},
		columnClickHandler(name, data) {
			this.$options.methods[name](this, data);
		},
		//查看
		async show1(_this, row) {
			let assetCategory = await _this.$cache.data.loadAssetCategory(row.assetCategoryId);
			try {
				_this.loading = true;
				let entity = {cardId: assetCategory.assetCardId, _editFlag: false}
				_this.$nextTick(() => {
					_this.cardPreview = false;
					_this.$refs.dataCard.open(Object.assign(entity, row));
				});
			} finally {
				_this.loading = false;
			}
		},
		//导出
		async export(_this, index) {
      let attachParams = {};
      let exportApi = _this.$API.asset.asset.export;
      let exTableRef = _this.$refs.table;
      let exportBtnRef = _this.$refs.button;
      let exportBtnIndex = index;
      await BaseTemplate.export(_this, exportApi, attachParams, exportBtnRef, exportBtnIndex, exTableRef);
		},
		//查看
		async import(_this,index) {
			try {
				_this.$refs.button.loading(index, true);
				_this.$nextTick(() => {
					_this.$refs.importTags.open({});
				});
			} finally {
				_this.$refs.button.loading(index, false);
			}
		},
		//标签打印
		async labelPrint(_this, index) {
			let selections = _this.$refs.table.selections();
			if (selections.length === 0) {
				let confirm = await _this.$confirm('是否全部标签打印', '提示', {type: 'warning'}).then(async () => {
					let queryParams = {}
					_this.$refs.button.loading(index, true);
					Object.assign(queryParams, _this.$refs.search.searchParams());
					queryParams.current = 1;
					queryParams.pageSize = _this.maxSelectdNumber;
					_this.$API.asset.asset.list.post(queryParams).then((res) => {
						if(res.code === 2000 && res.data.records){
							let assetCodes = [];
							res.data.records.forEach(item =>{
								assetCodes.push(item.code);
							})
							let data = {
								assetCodes:assetCodes,
							};
							_this.$nextTick(() => {
								_this.$refs.printReview.open(data);
							})
							_this.$refs.button.loading(index, false);
						}
					});
				}).catch(() => {
					_this.$refs.button.loading(index, false);
				});
					if(!confirm) return ;
				//是否全部标签打印?
			}else{
				_this.$confirm(`确定打印选择的 ${selections.length} 项吗？`, '警告', {
					type: 'warning'
				}).then(async () => {
					_this.$refs.button.loading(index, true);
					let assetCodes = [];
					for (let index = 0; index < selections.length; index++) {
						const element = selections[index];
						assetCodes.push(element.code);
					}

					let data = {
						assetCodes:assetCodes,
					};
					_this.$nextTick(() => {
						_this.$refs.printReview.open(data);
					})
					_this.$refs.button.loading(index, false);
				}).catch(() => {
					_this.$refs.button.loading(index, false);
				});
			}
		},

	}
}
</script>
